package cn.itsource.param.controller;


import cn.itsource.param.domain.Param;
import cn.itsource.param.query.ParamQuery;
import cn.itsource.param.service.IParamService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {

    @Autowired
    private IParamService service;
    @GetMapping
    public AjaxResult param(){
        try {

            return AjaxResult.me().setResultObj(service.param()).setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping
    public AjaxResult save(@RequestBody Param param){
        try {
            service.save(param);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.deleteByPrimaryKey(id);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *  分页查询
     * @param paramQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody ParamQuery paramQuery){
        try {
            return AjaxResult.me().setSuccess(true).setMessage("操作成功").setResultObj(service.pageInfo(paramQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    /**
     * 批量删除   由于批量删除 前端可以看到传过来的其实是一个一个对象 把这些对象装在一个集合里面接收
     * @param
     * @return
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Param> params){
        try {
            service.deleteByCollection(params);
            return  AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }
 /*   @PostMapping("/exist")
    public AjaxResult exist(@RequestBody Param param){
        try {
            return AjaxResult.me().setResultObj(service.exist(param));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败,请稍后再试!");
        }
    }*/


}
